package com.yanqu.road.server.logic.activity.condition.Advertisement;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.advertisement.AdvertisementUserData;
import com.yanqu.road.entity.activity.advertisement.config.AdvertisementConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.advertisement.AdvertisementActivityModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.advertisement.AdvertisementActivityMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;

public class TotalWatchAdvertisementTimeCondition extends BaseActivityCondition {


    public TotalWatchAdvertisementTimeCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        AdvertisementUserData userData = player.getModule(AdvertisementActivityModule.class).getUserData();
        AdvertisementConfig config = AdvertisementActivityMgr.getConfig();
        if (userData == null || config == null){
            return;
        }
        if (player.getTitleId() < config.getTotalWatchMinLevel()){
            return;
        }
        //过期了
        if (userData.getActiveTime() + config.getTotalWatchRewardActiveDay() * DateHelper.DAY_MILLIONS < System.currentTimeMillis()){
            return;
        }
        setValue(getValue().add(BigInteger.ONE));
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.TotalWatchAdvertisementTimeCondition.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.TotalWatchAdvertisementTimeCondition.getValue(), handle);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }
}
